package com.xncoding.echarts.common.util;

import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.List;

public class PdfTestCase{

    public static String parseHtmlToText(String strHtml) {
        if ((strHtml == null) || ("".equals(strHtml))) {
            return "";
        }
        String text = strHtml.replaceAll("<div><div>", "");
        text = text.replaceAll("</div></div>", "");
        text = text.replaceAll("&nbsp; &nbsp; &nbsp; &nbsp;", "  ");
        text = text.replaceAll("<br />", "\n");
        text = text.replaceAll("<br>", "\n");
        text = text.replaceAll("=\\\"[^\\\"]*\\\"", " ").replaceAll("\\<[^(\\<|\\>)]*\\>", " ");
        text = text.replaceAll("&nbsp;", " ");
        return text;
    }

    public static List<Element> parseHtmlToTextForFood(String strHtml)throws Exception
    {
        String text = strHtml.replaceAll("<div><div>", "");
        org.dom4j.Document doc = null;
        try {
            doc = DocumentHelper.parseText(text);
        } catch (org.dom4j.DocumentException e) {
            e.printStackTrace();
            throw new Exception("parseHtmlToTextForFood exception!生成pdf解析食物片段异常!");
        }
        List list = doc.selectNodes("table/tr/td");
        return list;
    }

    public static void main(String[] args)throws Exception{
        org.dom4j.Document doc = DocumentHelper.parseText("<table><tbody><tr><td>谷物类:薏苡仁 黑大豆 赤小豆 等。</td><td>肉食类:牛肉 兔肉 鹌鹑 等。</td></tr><tr><td>蔬菜类:扁豆 萝卜 冬瓜 等。</td><td>果品类:橄榄 </td></tr></tbody></table>");
        List<Element> list = doc.selectNodes("table/tbody/tr/td");
        for (Element e : list)
            System.out.println(e.getTextTrim());
    }
}

